package com.iespring.controller.admin.goods;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iespring.bean.admin.goods.bo.*;
import com.iespring.bean.admin.goods.po.MallGoods;
import com.iespring.bean.admin.goods.vo.GoodsInfoVo;
import com.iespring.bean.admin.goods.vo.UpdateGoodsInfoVo;
import com.iespring.bean.common.BasePageInfo;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.bean.common.CommonData;
import com.iespring.service.admin.goods.GoodsService;
import com.iespring.util.StrUtil;
import com.iespring.util.ValidationUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author KeFei
 * @description: 商品模块
 * @date 2022/12/29 9:46
 */
@RestController
@RequestMapping("admin")
public class GoodsController {

    @Resource
    GoodsService goodsService;

    /**
     * 显示商品列表
     * TODO 判断输入参数是否正确
     */
    @RequestMapping("goods/list")
    public BaseRespVo goodsList(BasePageInfo basePageInfo) {
        // 判断输入参数是否正确
        // Integer goodsId = basePageInfo.getGoodsId();
        // if () {
        //     return BaseRespVo.invalidNumber("参数不正确");
        // }

        CommonData data = null;
        // 用于推广模块显示所有商品列表
        if (basePageInfo.getLimit() == 0) {
            // 查询所有的商品信息
            List<MallGoods> goodsList = goodsService.showGoodsList(basePageInfo);
            return BaseRespVo.ok(goodsList);
        } else {
            // 开启分页查询
            PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
            // 查询所有的商品信息
            List<MallGoods> goodsList = goodsService.showGoodsList(basePageInfo);
            // 分页查询
            PageInfo<MallGoods> pageInfo = new PageInfo<>(goodsList);
            // 封装查询结果
            data = CommonData.data(pageInfo);
        }

        return BaseRespVo.ok(data);
    }

    /**
     * 获取商品上架前的所需信息
     */
    @RequestMapping("goods/catAndBrand")
    public BaseRespVo getGoodsInfo() {

        GoodsInfoVo goodsInfoVo = goodsService.getGoodsInfo();

        return BaseRespVo.ok(goodsInfoVo);
    }

    /**
     * 上架商品
     */
    @RequestMapping("goods/create")
    public BaseRespVo addGoods(@RequestBody @Valid AddGoodsBo addGoodsBo, BindingResult bindingResult) {
        ValidationUtils.validParam(bindingResult);
        // 判断输入参数是否正确
        GoodsBo goods = addGoodsBo.getGoods();
        if (StrUtil.isEmpty(goods.getGoodsSn()) || StrUtil.isEmpty(goods.getName())) {
            return BaseRespVo.invalidNumber("参数不正确");
        }
        goodsService.addGoods(addGoodsBo);

        return BaseRespVo.ok();
    }

    /**
     * 获取要编辑商品的详细信息
     */
    @RequestMapping("goods/detail")
    public BaseRespVo getUpdateGoodsInfo(Integer id) {

        UpdateGoodsInfoVo updateGoodsInfoVo = goodsService.getUpdateGoodsInfo(id);

        return BaseRespVo.ok(updateGoodsInfoVo);
    }

    /**
     * 更新编辑后的商品
     */
    @RequestMapping("goods/update")
    public BaseRespVo updateGoodsInfo(@RequestBody UpdateGoodsInfoBo updateGoodsInfoBo) {

        // 判断输入参数是否正确
        UpdateGoodsGoodsInfoBo goods = updateGoodsInfoBo.getGoods();
        if (StrUtil.isEmpty(goods.getGoodsSn()) || StrUtil.isEmpty(goods.getName())) {
            return BaseRespVo.invalidNumber("参数不正确");
        }

        goodsService.updateGoodsInfo(updateGoodsInfoBo);

        return BaseRespVo.ok();
    }

    /**
     * 删除商品
     */
    @RequestMapping("goods/delete")
    public BaseRespVo deleteGoods(@RequestBody DeleteGoodsBo deleteGoodsBo) {

        goodsService.deleteGoods(deleteGoodsBo);

        return BaseRespVo.ok();
    }

    /**
     * 显示商品评论列表
     * TODO 判断输入参数是否正确
     */
    @RequestMapping("comment/list")
    public BaseRespVo showCommentList(BasePageInfo basePageInfo) {
        // 判断输入参数是否正确
        // Integer goodsId = basePageInfo.getGoodsId();
        // if () {
        //     return BaseRespVo.invalidNumber("参数不正确");
        // }

        CommonData data = goodsService.showCommentList(basePageInfo);

        return BaseRespVo.ok(data);
    }

    /**
     * 回复商品评论
     */
    @RequestMapping("order/reply")
    public BaseRespVo replyComment(@RequestBody Map map) {

        // 判断回复内容是否为空
        String content = (String) map.get("content");
        if (StrUtil.isEmpty(content)) {
            return BaseRespVo.error("参数不正确", 401);
        }

        // 规定返回replyState=0,表示未回复;replyState=1,表示已回复
        int replyState = goodsService.replyComment(map);
        if (replyState == 1) {
            return BaseRespVo.error("订单商品已回复！", 622);
        }
        return BaseRespVo.ok();
    }

    /**
     * 删除评论
     */
    @RequestMapping("comment/delete")
    public BaseRespVo deleteComment(@RequestBody DeleteCommentBo deleteCommentBo) {

        goodsService.deleteComment(deleteCommentBo);

        return BaseRespVo.ok();
    }

}
